angular.module('starter.controllers')
.controller('CheckvipCtrl', function($scope, museumFac, $stateParams, $timeout, $ionicScrollDelegate, cfg, $location, $cookies, $interval, $state, uploadImg) {
	$scope.type = $stateParams.type;

  $scope.change_language = function(id){
	    $scope.language = id;
	    var expireDate = new Date();  
		expireDate.setDate(expireDate.getDate() + 30);                
		$cookies.put('language', id, {'expires': expireDate.toUTCString()});
		location.reload();
		if(id == 0){
			document.getElementById('title').innerHTML = '提示';
			document.getElementById('windowclosebutton').value = '确定';
		}else{
			document.getElementById('title').innerHTML = 'Hint';
			document.getElementById('windowclosebutton').value = 'Confirm';
		}

	}

	$scope.getLanguage = function(){
    	$scope.language = $cookies.get('language');
    	if($scope.language){
      		$scope.language = $scope.language;
    	}else{
      		$scope.language = 0;
    	}
    	if($scope.language == 0){
			document.getElementById('title').innerHTML = '提示';
			document.getElementById('windowclosebutton').value = '确定';
		}else{
			document.getElementById('title').innerHTML = 'Hint';
			document.getElementById('windowclosebutton').value = 'Confirm';
		}
  	}
  	$scope.getLanguage();

	$scope.checkOpenid = function(){
        museumFac.user.usertest(function(data){
          if(data.info == true){
            
          }else{
            $scope.url = cfg.baseUrl + 'home/index/vipcheckpay?type=' + $scope.type;
            $cookies.put('lastPage', $scope.url);
            console.log($scope.url);
            $location.url('login');
          }
        });

        wx.ready(function(){
          wx.onMenuShareTimeline({
            title: '四方当代美术馆 | 线上商店', // 分享标题
            desc: ' ', // 分享描述
            link: $scope.url, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
            imgUrl: 'http://store.sifangartmuseum.org.cn/Public/museum/www/img/logo.png', // 分享图标
            success: function () { 
                // 用户确认分享后执行的回调函数
            },
            cancel: function () { 
                // 用户取消分享后执行的回调函数
            }
        });
        // 获取“分享给朋友”按钮点击状态及自定义分享内容接口
        wx.onMenuShareAppMessage({
            title: '四方当代美术馆 | 线上商店',  // 分享标题
            desc: ' ', // 分享描述
            link: $scope.url, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
            imgUrl: 'http://store.sifangartmuseum.org.cn/Public/museum/www/img/logo.png', // 分享图标
            type: '', // 分享类型,music、video或link，不填默认为link
            dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
            success: function () { 
                // 用户确认分享后执行的回调函数
            },
            cancel: function () { 
                // 用户取消分享后执行的回调函数
            }
          });
        });
	}
	$scope.checkOpenid();

    $scope.backhome = function(){
      $location.url('home');
    }

    $scope.gotoCenter = function(){
      museumFac.user.usertest(function(data){
        if(data.info == true){
          $location.url('center');
        }else{
          $scope.url = window.location.href;
          $cookies.put('lastPage', $scope.url);
          $location.url('login');
        }
      });
    }

    $scope.getUser = function() {
      museumFac.user.profile(function(data){
        $scope.user2 = data.info;
      },function(data){
        alert(data.data.info);
      });
    };
    $scope.getUser();

    $scope.getfocus = function(){
        if($stateParams.type == 1){
            $scope.focuslists = [
                {title:'展览'},
                {title:'人文讲座'},
                {title:'山间电影院'},
                {title:'地形学闲逛'},
                {title:'四方夜厂'},
                {title:'其他'}
            ];
        }
        if($stateParams.type == 2){
            $scope.focuslists = [
                {title:'展览'},
                {title:'人文讲座'},
                {title:'山间电影院'},
                {title:'地形学闲逛'},
                {title:'四方夜厂'},
                {title:'其他'}
            ];
        }
        if($stateParams.type == 3){
            $scope.focuslists = [
                {title:'展览'},
                {title:'人文讲座'},
                {title:'山间电影院'},
                {title:'地形学闲逛'},
                {title:'四方夜厂'},
                {title:'亲子活动'},
                {title:'其他'}
            ];
        }
    }
    $scope.getfocus();

    $scope.user = {};

    $scope.check_vipname = function(){
      if(!$scope.user.name){
        $scope.name_mark = 1;
      }else{
        $scope.name_mark = 0;
      }
    }

    $scope.check_vipphone = function(){
      if(!$scope.user.phone){
        $scope.phone_mark = 1;
      }else{
        $scope.phone_mark = 0;
      }
    }

    $scope.check_vipemail = function(){
      var myReg=/^[a-zA-Z0-9_-]+@([a-zA-Z0-9]+\.)+(com|cn|net|org)$/;

      if(!$scope.user.email){
        $scope.email_mark = 1;
      }else{
        
        if(myReg.test($scope.user.email)){
  　　　　$scope.email_mark = 0;
    　　}else{
    　　　$scope.email_mark = 1;
        }

      }
    }

    $scope.check_vipidcard = function(){
      if(!$scope.user.idcard){
        $scope.idcard_mark = 1;
      }else{
        $scope.idcard_mark = 0;
      }
    }

    $scope.check_vipstudentcard = function(){
      if(!$scope.user.studentcard){
        $scope.studentcard_mark = 1;
      }else{
        $scope.studentcard_mark = 0;
      }
    }

    $scope.check_vipschool = function(){
      if(!$scope.user.school){
        $scope.school_mark = 1;
      }else{
        $scope.school_mark = 0;
      }
    }

    $scope.check_vipedu = function(){
      if(!$scope.user.edu){
        $scope.edu_mark = 1;
      }else{
        $scope.edu_mark = 0;
      }
    }

    $scope.check_vipjob = function(){

      if(!$scope.user.job){
        $scope.job_mark = 1;
        $('.fix2').css('color', '#9B9B9B');
      }else{
        $scope.job_mark = 0;
        $('.fix2').css('color', '#000');
      }
    }

    $scope.check_vipgender = function(){

      if(!$scope.user.gender){
        $scope.gender_mark = 1;
        $('.fix1').css('color', '#9B9B9B');
      }else{
        $scope.gender_mark = 0;
        $('.fix1').css('color', '#000');
      }
    }

    $scope.check_vipfamily = function(){
      if(!$scope.user.family_num){
        $scope.family_mark = 1;
      }else{
        $scope.family_mark = 0;
      }
    }

    $scope.choen = function(flag){
        $scope.inputc = flag;
    }

    $scope.choose_ticket = function(key,list){
        if(list.is_chosen == 1){
            list.is_chosen = 0;
        }else{
            list.is_chosen = 1;
        }
        
        var team = [];
        angular.forEach($scope.focuslists, function(item){
            if(item.is_chosen == 1){
                team.push(item.title);
            }
        });

        $scope.teamlength = team.length;

        $scope.user.focustype =team.toString();
    }

    $scope.getVipDetail = function(){
       museumFac.order.viptype({
        type: $stateParams.type
       },function(data){
        $scope.vipdetail = data.info;
       },function(data){
        alert(data.data.info);
       })
    }

    $scope.gopay = function(){
      if(!$scope.teamlength||$scope.teamlength<1){
        alert('请选择关注活动类型');
      }else{

        $scope.user.type = $stateParams.type;
        museumFac.wechat.vippay($scope.user, function(response) {
        var res = response;
        if (res.info) {
          alert(res.info);
          $state.go('tab.vipsuccess');
          return;
        }

        var jsApiCall = function(data) {
          WeixinJSBridge.invoke(
            'getBrandWCPayRequest',
            data,
            function(res) {
              WeixinJSBridge.log(res.err_msg);
              if (res.err_msg == 'get_brand_wcpay_request:ok') {
                  if($scope.language == 0){
                    document.getElementById('title').innerHTML = '提示';
                    document.getElementById('windowclosebutton').value = '确定';
                    $scope.successtext = "支付成功！";
                  }else{
                    document.getElementById('title').innerHTML = 'Hint';
                    document.getElementById('windowclosebutton').value = 'Confirm';
                    $scope.successtext = "Purchase succeeded!";
                  }
                  alert($scope.successtext);
                  $state.go('tab.vipsuccess');
                } else if (res.err_msg == 'get_brand_wcpay_request:cancel') {
                  if($scope.language == 0){
                    alert("已取消支付!");
                  }else{
                    alert("Payment canceled");
                  }
                  $state.go('tab.vip');
                } else if (res.err_msg == 'get_brand_wcpay_request:fail') {
                  if($scope.language == 0){
                    alert("支付失败!");
                  }else{
                    alert("Payment failed");
                  }
                  $state.go('tab.vip');
                } else {
                  if($scope.language == 0){
                    alert('支付失败' + res.err_msg);
                  }else{
                    alert('Payment failed' + res.err_msg);
                  }
                  $state.go('tab.vip');
                };
            }
          );
        }
        jsApiCall(res);
      }, function(data) {
        alert(data.data.info);
      });

      }
        
    }
  
})